package core;

import fileoperation.FileCreation;
import fileoperation.FileDeletion;
import repository.Repository;
import gitobject.Blob;
import gitobject.Tree;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class JitHash {
    /**
     * Generate a hash file for the specified file
     * @param filename
     * @throws IOException
     */

    /**
        This class is part of the original project, but in our project structure, this class is not called.
     */

    public static void hash(String fileName) throws Exception {
        /* Todo: You should pass the filename in this function, and generate a hash file in your repository.
        *   Add your code here.*/
        while(true){
            System.out.println("This class is part of the original project, but in our project structure, this class is not called. \n" +
                    "Warning: use this class to create hash files for specified files or folders, but this may lead to conflicts in our project structure. \n" +
                    "If you want to add files, please use the jit add directive. For more information on how to use it, please refer to the documentation. \n" +
                    "We do not recommend you to use this instruction, please enter 'Exit' to return. \n" +
                    "If you insist on using this instruction, please enter 'I understand that this may conflict with the project structure.'");
            Scanner input = new Scanner(System.in);
            String choice = input.nextLine();
            if(choice.equals("Exit"))
                return;
            else if(choice.equals("I understand that this may conflict with the project structure."))
                break;
        }

        createHashObject(fileName);
    }
    
    private static void createHashObject(String filename) throws Exception{
        File hashObject = new File(filename);

        // if it is a file
        if(hashObject.isFile()){
            Blob blobObject = new Blob(hashObject);
            blobObject.writeObject(); //Serialize the object to the local repository.

            // Compression write
//          String hashRes = blobObject.genKey(hashObject); // get hash value
//          FileCreation.createDirectory(blobObject.getPath(), hashRes.substring(0,2)); // create directory
//          FileCreation.createFile(blobObject.getPath() + File.separator + hashRes.substring(0,2),
//                    hashRes.substring(2), null); // create file
//          blobObject.compressWrite(); // Serialize the object and compress with zlib.
        }
        else if(hashObject.isDirectory()){
            // if it is a directory
            Tree treeObject = new Tree(hashObject);
            treeObject.writeObject();

            // Compression write
//          String hashRes = treeObject.genKey(hashObject); // get hash value
//          FileCreation.createDirectory(treeObject.getPath(), hashRes.substring(0,2)); // create directory
//          FileCreation.createFile(treeObject.getPath() + File.separator + hashRes.substring(0,2),
//                    hashRes.substring(2), null); // create file
//          treeObject.compressWrite(); // Serialize the object and compress with zlib.

            File[] fs = hashObject.listFiles();
            for(int i=0; i < fs.length; i++){
                createHashObject(fs[i].getName());
            }

        }
    }
}
